#no
import time
from matplotlib.widgets import Slider
import matplotlib.pyplot as plt
from pylab import mpl
import pandas as pd
import numpy as np
from databases_lib import sqlite3_select,sqlite3_select_time
from mytools import calculate_time


@calculate_time
def a1(listx,listy):


    plt.rcParams['font.sans-serif'] = ['SimHei']  # 运行配置参数中的字体（font）为黑体（SimHei）


    fig, ax = plt.subplots(figsize=(12, 6))
    plt.subplots_adjust(bottom=0.25)  # 为滑块留出空间

    line, = ax.plot(listx, listy, linewidth=0.5)
    ax.set_title("可滚动的超长图表")
    ax.grid(True)

    #plt.plot(listx, listy,linewidth=0.5) # 绘制基础折线图
    #plt.bar(listx, listy, linewidth=0.5,label='条形图')
    #plt.scatter(listx, listy,s=3,  c='blue', alpha=0.6,  edgecolors='black')  # 散点图

    plt.grid(True, alpha=0.3)
    plt.tight_layout()  # 自动调整子图参数


    plt.title('123')
    plt.xlabel('单位：time')
    plt.xticks(rotation=-75)
    plt.ylabel('单位：db +59')

    # 创建滑块
    ax_slider = plt.axes([0.2, 0.1, 0.6, 0.03])
    slider = Slider(ax_slider, '位置', 0, len(listx), valinit=0, valstep=100)

    def update(val):
        pos = int(slider.val)
        window_size = 1000
        ax.set_xlim(pos, pos + window_size)
        fig.canvas.draw_idle()

    slider.on_changed(update)
    plt.show() #block=False
    #time.sleep(10)
    #plt.close()




if __name__ == '__main__':
    data = sqlite3_select_time()
    listx = []
    listy = []
    for i in data:
        listx.append(i[1])
        listy.append(int(i[2])-50)
    arr_x = np.array(listx)
    arr_y = np.array(listy)
    print(listy)
    a1(arr_x,arr_y)

